<!DOCTYPE html>
<html>
<head>
    <title>入院办理--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/disableHyperlink.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>
    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>

</head>
<body>

<form id="searchForm" method="post" class="definewidth m20">
    <input name="index" style="visibility: hidden">
    <input name="size" value="5" style="visibility: hidden">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">病例号：</td>
            <td><input type="text" name="id" value=""/></td>

            <td width="10%" class="tableleft">主治医生：</td>
            <td><input type="text" name="doctorName" value=""/></td>

            <td width="10%" class="tableleft">科室：</td>
            <td><input type="text" name="departmentName" value=""/></td>
        </tr>
        <tr>

            <td width="10%" class="tableleft">住院时间：</td>

            <td colspan="5">
                <input name="startTime" type="datetime-local" value=""/>&nbsp;至&nbsp;<input type="datetime-local"
                                                                                            name="endTime" value=""/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button class="btn btn-primary" type="button" onclick="loadData(1)">查询</button>
                <button class="btn btn-primary" type="reset">清空</button>

            </td>
        </tr>
    </table>
</form>
<form id="deleteForm">
    <table class="table table-bordered table-hover definewidth m10">
        <thead>
        <tr>
            <th><input type="checkbox" id="checkall" onchange="checkAll();"></th>
            <th>病历号</th>
            <th>姓名</th>
            <th>床位号</th>
            <th>联系电话</th>
            <th>押金</th>
            <th>主治医生</th>
            <th>入院时间</th>
            <th>科室</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>

        <tbody id="hosList"></tbody>

    </table>
</form>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div class="inline pull-right page">
                <a id="firstPage">第一页</a>
                <a id="prePage">上一页</a>
                <span id="pageList">
                </span>
                <a id="nextPage">下一页</a>
                <a id="lastPage">最后一页</a>
                &nbsp;&nbsp;&nbsp;共<span id="total">32</span>条记录<span class='current'> <span
                    id="current_page">1</span>/<span id="total_page">33 </span></span>页
            </div>
            <div>
                <button type="button" class="btn btn-success" id="newNav">添加住院信息</button>&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-success" id="outHospital" onclick="hos('out');">出院</button>&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-success" id="exitHospital" onclick="hos('exit');">退院</button>&nbsp;&nbsp;&nbsp;
                <button type="button" class="btn btn-success" id="delPro">导出Excel</button>
            </div>

        </th>
    </tr>
</table>

</body>
</html>
<script>

    function checkAll() {
        var alls = document.getElementsByName("patients");
        var ch = document.getElementById("checkall");
        if (ch.checked) {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = true;
            }
        } else {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = false;
            }
        }
    }

    function delAll() {
        var alls = document.getElementsByName("check");
        var ids = new Array();
        for (var i = 0; i < alls.length; i++) {
            if (alls[i].checked) {
                ids.push(alls[i].value);
            }
        }
        if (ids.length > 0) {
            if (confirm("确认操作?")) {
                alert("成功!");
            }
        } else {
            alert("请选中要操作的项");
        }
    }


    function hosById(path, id) {
        if (confirm("确认操作?")) {
            $.ajax({
                type: "POST",
                url: "/hospitals/" + path,
                data: "patients=" + id + "&_method=PUT",
                success: function (vo) {
                    alert(vo.obj);
                    window.location.href = "index.html";
                }
            })
        }
    }

    function hos(path) {
        let patients = document.getElementsByName("patients");
        let ids = new Array();
        for (let i = 0; i < patients.length; i++) {
            if (patients[i].checked) {
                ids.push(patients[i].value);
            }
        }
        if (ids.length > 0) {
            if (confirm("确认操作?")) {
                $.ajax({
                    type: "POST",
                    url: "/hospitals/" + path,
                    data: $("#deleteForm").serialize() + "&_method=PUT",
                    success: function (vo) {
                        alert(vo.obj);
                        window.location.href = "index.html";
                    }
                })
            }
        } else {
            alert("请选中要操作的项");
        }
    }

    function fillPageData(pageInfo) {
        let id = $("input[name='id']").val();
        let doctorName =escape($("input[name='doctorName']").val());
        let departmentName = escape($("input[name='departmentName']").val());
        let startTime = $("input[name='startTime']").val();
        let endTime = $("input[name='endTime']").val();
        let data = "&id=" + id + "&doctorName=" + doctorName + "&departmentName=" + departmentName + "&startTime=" + startTime + "&endTime=" + endTime;

        $("#total").html(pageInfo.total);
        $("#total_page").html(pageInfo.pages);
        $("#current_page").html(pageInfo.pageNum);
        $("input[name='index']").val(pageInfo.pageNum);

        $("#firstPage").attr('href', 'index.html?index=' + 1 + data);
        $("#prePage").attr('href', 'index.html?index=' + (pageInfo.pageNum - 1) + data);
        $("#nextPage").attr('href', 'index.html?index=' + (pageInfo.pageNum + 1) + data);
        $("#lastPage").attr('href', 'index.html?index=' + (pageInfo.pages) + data);
        $("#firstPage").removeAttr("disabled")
        $("#prePage").removeAttr("disabled")
        $("#nextPage").removeAttr("disabled")
        $("#lastPage").removeAttr("disabled")

        if (pageInfo.isFirstPage) {
            $("#firstPage").attr("disabled", true);
            $("#prePage").attr("disabled", true);
        }

        if (pageInfo.isLastPage) {
            $("#nextPage").attr("disabled", true);
            $("#lastPage").attr("disabled", true);
        }
        let str = "";
        let low = pageInfo.pageNum - 2;
        let high = pageInfo.pageNum + 2;
        if (low >= 1 && high <= pageInfo.pages) {
            for (let i = low; i <= high; i++) {
                str += "<a href='index.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
            }
        } else if (low < 1) {
            if (pageInfo.pages == 0)
                str += "<a href='index.html?index=1" + data + "' disabled=true>1</a>";
            else {
                for (let i = 1; i <= (pageInfo.pages < 5 ? pageInfo.pages : 5); i++) {
                    str += "<a href='index.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
                }
            }
        } else if (high > pageInfo.pages) {
            for (let i = (pageInfo.pages - 5 >= 1 ? pageInfo.pages - 4 : 1); i <= pageInfo.pages; i++) {
                str += "<a href='index.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
            }
        }
        $("#pageList").html(str)
    }

    function loadData(index) {

        if (index == null) {
            let url = document.location.toString();
            index = url.GetValue("index");
            if (index == null) {
                index = 1;
            }
            let id = url.GetValue("id");
            let doctorName = url.GetValue("doctorName");
            let startTime = url.GetValue("startTime");
            let departmentName = url.GetValue("departmentName");
            let endTime = url.GetValue("endTime");
            if (id != null)
                $("input[name='id']").val(id);
            if (doctorName != null)
                $("input[name='doctorName']").val(doctorName);
            if (departmentName != null)
                $("input[name='departmentName']").val(departmentName);
            if (startTime != null)
                $("input[name='startTime']").val(startTime);
            if (endTime != null)
                $("input[name='endTime']").val(endTime);
        }
        $("input[name='index']").val(index)
        let data = $("#searchForm").serialize();
        if (data.GetValue("startTime") != null) {
            data = data.SetValue("startTime", data.GetValue("startTime").replaceAll("T", " "))
        }
        if (data.GetValue("endTime") != null) {
            data = data.SetValue("endTime", data.GetValue("endTime").replaceAll("T", " "))
        }
        $.ajax(
            {
                type: "GET",
                data: data,
                url: "/hospitals/list",
                dataType: "json",
                success: function (vo) {
                    let str = "";
                    $("#hosList").html(str);
                    if (vo.code == 200) {
                        let list = vo.pageInfo.list;
                        for (let i = 0; i < list.length; i++) {
                            let obj = list[i];
                            let pricePeopleList = obj.pricePeople;
                            let total = 0;
                            for (let i = 0; i < pricePeopleList.length; i++) {
                                let pricePeople = pricePeopleList[i];
                                total += pricePeople.chargeProject.money;
                            }
                            let deposit = obj.deposit;
                            let remain = deposit - total;
                            str += "<tr>" +
                                "<td style='vertical-align:middle;'>" +
                                (obj.state == 0 ? ("<input type='checkbox' name='patients' value='" + obj.id + "' />") : "")
                                + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.id + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.name + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.bedId + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.phone + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.deposit + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.doctor.name + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.inTime + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.doctor.department.name + "</td>" +
                                "<td style='vertical-align:middle;'>" + (obj.state == 0 ? "已住院" : obj.state == 1 ? "已退院" : "已出院") + "</td>" +
                                "<td style='vertical-align:middle;'>" +
                                // TODO: 这里还差判断存款是否不够，如果不够再显示存款按钮
                                "<a href='look.html?pre=index&id=" + obj.id + "'>详情>></a>" + (remain < 0&&obj.state==0 ?  "  <a href='add_deposit.html?id=" + obj.id + "'>存款</a>  ":"") +
                                (obj.state == 0 ? "<a style='cursor:pointer;' onclick=\"hosById('exit'," + obj.id + ")\">退院</a>  <a style='cursor:pointer;' onclick=\"hosById('out'," + obj.id + ")\">出院</a>" : "") +
                                "</td>" +
                                "</tr>";
                        }
                        $("#hosList").html(str)
                        fillPageData(vo.pageInfo);
                    } else {
                        alert("数据加载失败")
                    }
                }
            }
        )
    }

    $(function () {
        $('#newNav').click(function () {
            window.location.href = "add.html";
        });
        loadData();
    })
</script>